package com.iwinding.ladygg.net;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.iwinding.ladygg.constant.Constant;
import com.iwinding.ladygg.constant.SPConstant;
import com.iwinding.ladygg.net.callback.HeadImgCallback;
import com.iwinding.ladygg.net.request.AddFriendReq;
import com.iwinding.ladygg.net.request.AuthorizeReq;
import com.iwinding.ladygg.net.request.BindingMobileReq;
import com.iwinding.ladygg.net.request.CaptchaReq;
import com.iwinding.ladygg.net.request.ChangeMobileReq;
import com.iwinding.ladygg.net.request.ChangePasswordReq;
import com.iwinding.ladygg.net.request.DealSOSReq;
import com.iwinding.ladygg.net.request.Findpwd;
import com.iwinding.ladygg.net.request.FindpwdReset;
import com.iwinding.ladygg.net.request.ForeignRegisterReq;
import com.iwinding.ladygg.net.request.LoginReq;
import com.iwinding.ladygg.net.request.NoNoticeReq;
import com.iwinding.ladygg.net.request.PushSosReq;
import com.iwinding.ladygg.net.request.RegisterReq;
import com.iwinding.ladygg.net.request.ResourcesReq;
import com.iwinding.ladygg.net.request.SearchFriendReq;
import com.iwinding.ladygg.net.request.SendSOSReq;
import com.iwinding.ladygg.net.request.SetNoticesReq;
import com.iwinding.ladygg.net.request.SosRecordReq;
import com.iwinding.ladygg.net.request.ThirdPartyLoginReq;
import com.iwinding.ladygg.net.request.TokenReq;
import com.iwinding.ladygg.net.request.UpdatePersonInfoReq;
import com.iwinding.ladygg.okhttputils.OkHttpUtils;
import com.iwinding.ladygg.okhttputils.callback.Callback;
import com.iwinding.ladygg.utils.DateUtil;
import com.iwinding.ladygg.utils.GsonUtil;
import com.iwinding.ladygg.utils.SPUtils;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import okhttp3.ResponseBody;

/**
 * Created by hezhiyong on 2017/3/24.
 */

public class RequestManager {

    /**
     * 获取code
     *
     * @param authorizeReq
     * @param callback
     */
    public static void postAuthorize(AuthorizeReq authorizeReq, Callback callback) {
        OkHttpUtils
                .post()
                .url(Constant.NetUrl.PROHTTP+Constant.NetUrl.AUTHORIZE)
                .addParams("client_id", authorizeReq.client_id)
                .addParams("response_type", authorizeReq.response_type)
                .addParams("reqTime", authorizeReq.reqTime)
                .build()
                .execute(callback);
    }

    /**
     * 获取code
     *
     * @param authorizeReq
     */
    public static ResponseBody postAuthorize(AuthorizeReq authorizeReq) throws IOException {
        return OkHttpUtils
                .post()
                .url(Constant.NetUrl.PROHTTP+Constant.NetUrl.AUTHORIZE)
                .addParams("client_id", authorizeReq.client_id)
                .addParams("response_type", authorizeReq.response_type)
                .addParams("reqTime", authorizeReq.reqTime)
                .build()
                .execute().body();
    }



    /**
     * 获取token
     *
     * @param tokenReq
     * @param callback
     */
    public static void postAccessToken(TokenReq tokenReq, Callback callback) {
        OkHttpUtils
                .post()
                .url(Constant.NetUrl.PROHTTP+Constant.NetUrl.ACCESSTOKEN)
                .addParams("client_id", tokenReq.client_id)
                .addParams("client_secret", tokenReq.client_secret)
                .addParams("code", tokenReq.code)
                .addParams("grant_type", tokenReq.grant_type)
                .addParams("redirect_uri", tokenReq.redirect_uri)
                .addParams("reqTime", tokenReq.reqTime)
                .build()
                .execute(callback);
    }

    /**
     * 获取token
     *
     * @param tokenReq
     */
    public static ResponseBody postAccessToken(TokenReq tokenReq) throws IOException {
        return OkHttpUtils
                .post()
                .url(Constant.NetUrl.PROHTTP+Constant.NetUrl.ACCESSTOKEN)
                .addParams("client_id", tokenReq.client_id)
                .addParams("client_secret", tokenReq.client_secret)
                .addParams("code", tokenReq.code)
                .addParams("grant_type", tokenReq.grant_type)
                .addParams("redirect_uri", tokenReq.redirect_uri)
                .addParams("reqTime", tokenReq.reqTime)
                .build()
                .execute().body();
    }

    /**
     * 获取Resources
     *
     * @param resourcesReq
     * @param callback
     */
    public static void postResources(Context mContext, ResourcesReq resourcesReq, Callback callback) {
        OkHttpUtils
                .post()
                .url(Constant.NetUrl.PROHTTP+Constant.NetUrl.RESOURCES)
                .addHeader("Cookie","JSESSIONID="+ SPUtils.getSpPropertyString(mContext, SPConstant.COOKIE,""))
                .addParams("json", GsonUtil.getInstance().toJson(resourcesReq))
                .addParams("access_token", SPUtils.getToken(mContext))
                .build()
                .execute(callback);
    }

    public static void postResources(Context mContext,String uri, Object object, Callback callback) {
        ResourcesReq resourcesReq = new ResourcesReq();
        resourcesReq.uri = uri;
        resourcesReq.reqTime = String.valueOf(DateUtil.getCurrentTime());
        if (object != null)
            resourcesReq.params = GsonUtil.getInstance().toJson(object);

        postResources(mContext,resourcesReq, callback);
    }

    /**
     * 用户注册获取验证码
     *
     * @param req
     * @param callback
     */
    public static void postCaptcha(Context mContext,CaptchaReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.CAPTCHA, req, callback);
    }

    /**
     * 用户注册
     *
     * @param req
     * @param callback
     */
    public static void postRegister(Context mContext, RegisterReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.REGISTER, req, callback);
    }

    /**
     * 国外用户注册
     *
     * @param req
     * @param callback
     */
    public static void postForeignRegister(Context mContext,ForeignRegisterReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.FOREIGN_REGISTER, req, callback);
    }

    /**
     * 登录
     *
     * @param req
     * @param callback
     */
    public static void postLogin(Context mContext,LoginReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.LOGIN, req, callback);
    }

    /**
     * 第三方登录
     *
     * @param req
     * @param callback
     */
    public static void postThirdPartyLogin(Context mContext, ThirdPartyLoginReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.THIRDPARTY_LOGIN, req, callback);
    }

    /**
     * 绑定手机号获取验证码
     *
     * @param req
     * @param callback
     */
    public static void postBindingMobileCaptcha(Context mContext,CaptchaReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.BINDINGMOBILE_CAPTCHA, req, callback);
    }

    /**
     * 绑定手机号
     *
     * @param req
     * @param callback
     */
    public static void postBindingMobile(Context mContext, BindingMobileReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.BINDINGMOBILE, req, callback);
    }

    /**
     * 找回密码获取验证码
     *
     * @param req
     * @param callback
     */
    public static void postFindpwdCaptcha(Context mContext,CaptchaReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.FINDPWD_CAPTCHA, req, callback);
    }

    /**
     * 找回密码
     *
     * @param req
     * @param callback
     */
    public static void postFindpwd(Context mContext,Findpwd req, Callback callback) {
        postResources(mContext,Constant.NetUrl.FINDPWD, req, callback);
    }

    /**
     * 找回密码设置密码
     *
     * @param req
     * @param callback
     */
    public static void postFindpwdReset(Context mContext, FindpwdReset req, Callback callback) {
        postResources(mContext,Constant.NetUrl.FINDPWD_RESET, req, callback);
    }

    /**
     * 登出
     *
     * @param callback
     */
    public static void postLogout(Context mContext,Callback callback) {
        postResources(mContext,Constant.NetUrl.LOGOUT, null, callback);
    }

    /**
     * 通讯录
     *
     * @param callback
     */
    public static void postContacts(Context mContext,Callback callback) {
        postResources(mContext,Constant.NetUrl.CONTACTS, null, callback);
    }

    /**
     * 查找好友
     *
     * @param req
     * @param callback
     */
    public static void postSearchFriend(Context mContext,SearchFriendReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.SEARCH_FRIEND, req, callback);
    }

    /**
     * 添加好友
     *
     * @param req
     * @param callback
     */
    public static void postAddFriend(Context mContext, AddFriendReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.ADD_FRIEND, req, callback);
    }

    /**
     * 我的名片
     *
     * @param callback
     */
    public static void postMyCard(Context mContext,Callback callback) {
        postResources(mContext,Constant.NetUrl.MYCARD, null, callback);
    }

    /**
     * 我
     *
     * @param callback
     */
    public static void postMine(Context mContext,Callback callback) {
        postResources(mContext,Constant.NetUrl.MINE, null, callback);
    }

    /**
     * 个人信息
     *
     * @param callback
     */
    public static void postPersonInfo(Context mContext,Callback callback) {
        postResources(mContext,Constant.NetUrl.PERSONINFO, null, callback);
    }

    /**
     * 修改个人信息
     *
     * @param req
     * @param callback
     */
    public static void postUpdatePersonInfo(Context mContext,UpdatePersonInfoReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.UPDATE_PERSONINFO, req, callback);
    }

    /**
     * 提示人
     *
     * @param callback
     */
    public static void postNotices(Context mContext,Callback callback) {
        postResources(mContext,Constant.NetUrl.NOTICES, null, callback);
    }

    /**
     * 修改密码
     *
     * @param req
     * @param callback
     */
    public static void postChangePassword(Context mContext, ChangePasswordReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.CHANGEPASSWORD, req, callback);
    }

    /**
     * 设置提示人
     *
     * @param req
     * @param callback
     */
    public static void postSetNotices(Context mContext,SetNoticesReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.SETNOTICES, req, callback);
    }

//    /**
//     * 上传
//     * @param mContext
//     * @param url
//     * @param fileName
//     * @param callback
//     */
//    public static void postFile(Activity mContext, String url, String fileName, Callback callback)
//    {
//        File file = new File(Environment.getExternalStorageDirectory(), fileName);
//        if (!file.exists())
//        {
//            Toast.makeText(mContext, "文件不存在，请修改文件路径", Toast.LENGTH_SHORT).show();
//            return;
//        }
//        OkHttpUtils
//                .postFile()
//                .url(url)
//                .file(file)
//                .build()
//                .execute(callback);
//    }

    /**
     * 上传头像
     *
     * @param mContext
     * @param fileName
     * @param callback
     */
    public static void postHeadImg(final Activity mContext, String fileName, final Callback callback) {
//        File file = new File(Environment.getExternalStorageDirectory(), fileName);
        final File file = new File(fileName);
        if (!file.exists()) {
//            Toast.makeText(mContext, "文件不存在，请修改文件路径", Toast.LENGTH_SHORT).show();
            if(callback instanceof HeadImgCallback){
                ((HeadImgCallback)callback).onFail("文件不存在，请修改文件路径");
            }
            return;
        }

        ResourcesReq resourcesReq = new ResourcesReq();
        resourcesReq.uri = Constant.NetUrl.UPLOADHEADIMG;
        resourcesReq.reqTime = DateUtil.getCurrentTimeInString();
//        HeadImgReq req = new HeadImgReq();
//        req.headImg = ImageFormatTools.getInstance().file2Bytes(file);
//        resourcesReq.params =GsonUtil.getInstance().toJson(req);
//
        String json=null;
        try {
            json = URLEncoder.encode(GsonUtil.getInstance().toJson(resourcesReq),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String url = Constant.NetUrl.PROHTTP+Constant.NetUrl.RESOURCES+"?access_token="+SPUtils.getToken(mContext);
        if(!TextUtils.isEmpty(json)){
            url +="&json="+json;
        }
        OkHttpUtils
                .post()
                .addFile("headImg",DateUtil.getCurrentTimeInString()+fileName.substring(fileName.lastIndexOf(".")),file)
//                .url(Constant.NetUrl.PROHTTP+Constant.NetUrl.RESOURCES)
                .url(url)
                .addHeader("Cookie","JSESSIONID="+SPUtils.getSpPropertyString(mContext, SPConstant.COOKIE,""))
//                .addParams("json", GsonUtil.getInstance().toJson(resourcesReq))
//                .addParams("access_token", SPUtils.getToken(mContext))
                .build()
                .execute(callback);

//        OkHttpUtils
//                .post()
//                .addFile("headImg",DateUtil.getCurrentTimeInString(),file)
//                .url(Constant.NetUrl.PROHTTP+Constant.NetUrl.RESOURCES)
//                .addHeader("Cookie","JSESSIONID="+SPUtils.getSpPropertyString(mContext, SPConstant.COOKIE,""))
//                .addHeader("User-Agent", "iCane/1.0(Android)")
//                .addParams("access_token", SPUtils.getToken(mContext))
//                .addParams("json", URLEncoder.encode(GsonUtil.getInstance().toJson(resourcesReq)))
//                .build()
//                .execute(callback);

//        new Thread(){
//            @Override
//            public void run() {
//                super.run();
//                UploadNet.upload(mContext,file);
//            }
//        }.start();

    }

    /**
     * 更换手机
     *
     * @param req
     * @param callback
     */
    public static void postChangeMobile(Context mContext,ChangeMobileReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.CHANGEMOBILE, req, callback);
    }

    /**
     * 更换手机校验
     *
     * @param req
     * @param callback
     */
    public static void postChangeMobileCheck(Context mContext,ChangeMobileReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.CHANGEMOBILE_CHECK, req, callback);
    }

    /**
     * 更换手机获取验证码
     *
     * @param req
     * @param callback
     */
    public static void postChangeMobileCaptcha(Context mContext,CaptchaReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.CHANGEMOBILE_CAPTCHA, req, callback);
    }

    /**
     * Sos记录
     *
     * @param req
     * @param callback
     */
    public static void postSosRecord(Context mContext,SosRecordReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.SOSRECORD, req, callback);
    }

    /**
     * 发送SOS信息
     *
     * @param req
     * @param callback
     */
    public static void postSendSOS(Context mContext, SendSOSReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.SENDSOS, req, callback);
    }

    /**
     * 推送SOS信息
     *
     * @param req
     * @param callback
     */
    public static void postPushSos(Context mContext,PushSosReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.PUSHSOS, req, callback);
    }

    /**
     * SOS详情
     *
     * @param req
     * @param callback
     */
    public static void postSosDetail(Context mContext,PushSosReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.SOSDETAIL, req, callback);
    }

    /**
     * 处理SOS
     *
     * @param req
     * @param callback
     */
    public static void postDealSOS(Context mContext,DealSOSReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.DEALSOS, req, callback);
    }

    /**
     * 取消发送SOS
     *
     * @param req
     * @param callback
     */
    public static void postCancelSOS(Context mContext,DealSOSReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.CANCELSOS, req, callback);
    }

    /**
     * 取消关注SOS
     *
     * @param req
     * @param callback
     */
    public static void postNoNotice(Context mContext,NoNoticeReq req, Callback callback) {
        postResources(mContext,Constant.NetUrl.NONOTICE, req, callback);
    }

//    /**
//     * 获取Resources
//     *
//     * @param resourcesReq
//     * @param callback
//     */
//    public static void getResources(ResourcesReq resourcesReq, Callback callback) {
//        OkHttpUtils
//                .get()
//                .url(Constant.NetUrl.RESOURCES)
//                .addParams("uri", resourcesReq.uri)
//                .addParams("reqTime", resourcesReq.reqTime)
//                .addParams("params", TextUtils.isEmpty(resourcesReq.params) ? "" : resourcesReq.params)
//                .build()
//                .execute(callback);
//    }
//
//    public static void getResources(String uri, Object object, Callback callback) {
//        ResourcesReq resourcesReq = new ResourcesReq();
//        resourcesReq.uri = uri;
//        resourcesReq.reqTime = DateUtil.getCurrentTimeInString();
//        if (object != null)
//            resourcesReq.params = GsonUtil.getInstance().toJson(object);
//
//        getResources(resourcesReq, callback);
//    }
}
